package com.ruoyi.system.task;

import com.ruoyi.common.core.domain.entity.SysProject;
import com.ruoyi.system.domain.MaterialEntity;
import com.ruoyi.system.domain.MaterialHistoryStatisticsEntity;
import com.ruoyi.system.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 统计信息定时任务处理
 * --年 月 日产量统计
 */
@Component
@EnableScheduling
public class MainTask {
    @Autowired
    private ISysAdminProjectService sysAdminProjectService;
    @Autowired
    private IMainService mainService;


    @Autowired
    private MaterialService materialService;

    @Autowired
    private MaterialHistoryService materialHistoryService;

    private static final Logger logger = LoggerFactory.getLogger(MainTask.class);

    @PostConstruct
    public void Init() {
        yearTask();
        monthTask();
        dayTask();
    }

    /**
     * 各项目年度目标计算,每年执行一次
     */
    @Scheduled(cron = "0 0 2 1 1 ?")
    public void yearTask() {
        List<SysProject> projectList = sysAdminProjectService.list();
        projectList.forEach(item -> {
            mainService.countProjectYearProd(item.getId());
        });
    }

    /**
     * 各项目月度目标计算,每月执行一次
     */
    @Scheduled(cron = "0 0 1 1 1/1 ?")
    public void monthTask() {
        List<SysProject> projectList = sysAdminProjectService.list();
        projectList.forEach(item -> {
            mainService.countProjectMonthProd(item.getId());
        });
    }

    /**
     * 各项目每天目标计算,每天执行一次
     */
    @Scheduled(cron = "0 0 0 1/1 * ?")
    public void dayTask() {
        List<SysProject> projectList = sysAdminProjectService.list();
        projectList.forEach(item -> {
            mainService.countProjectDayProd(item.getId());
        });
    }

    /**
     *每天 23:58 分将 当前材料量保存一份到 t_material_history_tab 中
     */
    @Scheduled(cron = "58 * * * * ?")
    public void dayStatisticsMaterialNumTask() {
        logger.info("************************************************** 开始 copy 天级别材料数量 *****************************************************************");
        List<MaterialEntity> materialEntities = materialService.list();
        materialEntities.forEach(item -> {
            MaterialHistoryStatisticsEntity materialHistoryStatisticsEntity = new MaterialHistoryStatisticsEntity();

            materialHistoryStatisticsEntity.setMaterialId(item.getId());
            materialHistoryStatisticsEntity.setName(item.getName());
            materialHistoryStatisticsEntity.setNum(item.getNum());

            LocalDate today = LocalDate.now();
            // 定义日期格式
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            // 格式化日期
            String formattedDate = today.format(formatter);
            materialHistoryStatisticsEntity.setCreateTime(formattedDate);

            logger.info("Logging materialHistoryStatisticsEntity: {}", materialHistoryStatisticsEntity);
            materialHistoryService.addOrUpdate(materialHistoryStatisticsEntity);

        });

        logger.info("************************************************** copy 天级别材料数量完成 *****************************************************************");
    }
}
